// page/cart/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carts: [],
    havegoods:false,
    goods:[{
      
    }],
    
    isAllSelect: false,
    totalMoney: 0,
  },
//勾选事件处理函数  
switchSelect: function (e) {
  // 获取item项的id，和数组的下标值  
  console.log(e)
  var Allprice = 0,  i = 0;
  let id = e.target.dataset.id,

    index = parseInt(e.target.dataset.index);
  this.data.carts[index].isSelect = !this.data.carts[index].isSelect;
  //价钱统计
  if (this.data.carts[index].isSelect) {
    this.data.totalMoney = this.data.totalMoney + (this.data.carts[index].price * this.data.carts[i].count);
  }
  else {
    this.data.totalMoney = this.data.totalMoney - (this.data.carts[index].price * this.data.carts[i].count);
  }
  //是否全选判断
  for (i = 0; i < this.data.carts.length; i++) {
    Allprice = Allprice + (this.data.carts[index].price);
  }
  if (Allprice == this.data.totalMoney) {
    this.data.isAllSelect = true;
  }
  else {
    this.data.isAllSelect = false;
  }
  this.priceCount();
  this.setData({
    carts: this.data.carts,
    totalMoney: this.data.totalMoney,
    isAllSelect: this.data.isAllSelect,
  })
},
//全选
allSelect: function (e) {
  //处理全选逻辑
  let i = 0;
  if (!this.data.isAllSelect) {
    this.data.totalMoney = 0;
    for (i = 0; i < this.data.carts.length; i++) {
      this.data.carts[i].isSelect = true;
      this.data.totalMoney = this.data.totalMoney + (this.data.carts[i].price * this.data.carts[i].count);

    }
  }
  else {
    for (i = 0; i < this.data.carts.length; i++) {
      this.data.carts[i].isSelect = false;
    }
    this.data.totalMoney = 0;
  }
  this.setData({
    carts: this.data.carts,
    isAllSelect: !this.data.isAllSelect,
    totalMoney: this.data.totalMoney,
  })
},
// 去结算
toBuy() {
  wx.showToast({
    title: '去结算',
    icon: 'success',
    duration: 3000
  });
  this.setData({
    showDialog: !this.data.showDialog
  });
},
/* 减数 */
delCount: function (e) {
  var index = e.target.dataset.index;
  console.log("刚刚您点击了加一");
  var count = this.data.carts[index].count;
  // 商品总数量-1
  if (count > 1) {
    this.data.carts[index].count--;
    this.data.carts[index].totalMoney= this.data.carts[index].count *  this.data.carts[index].price;
  }
  // 将数值与状态写回  
  this.setData({
    carts: this.data.carts
  });
  console.log(this.data.carts[index]);
  this.priceCount();
},
/* 加数 */
addCount: function (e) {
  var index = e.target.dataset.index;
  console.log("刚刚您点击了加+");
  var count = this.data.carts[index].count;
  // 商品总数量+1  
  if (count < 10) {
    this.data.carts[index].count++;
    this.data.carts[index].totalMoney= this.data.carts[index].count *  this.data.carts[index].price;
  }
 
  // 将数值与状态写回  
  this.setData({
    carts: this.data.carts
  }); 
  console.log(this.data.carts[index]);

  this.priceCount();
},
priceCount: function (e) {
 
  this.data.totalMoney = 0;
  for (var i = 0; i < this.data.carts.length; i++) {
  
    if (this.data.carts[i].isSelect == true) {
      // console.log(this.data.carts[i].totalMoney);
      this.data.totalMoney = this.data.totalMoney + (this.data.carts[i].totalMoney);
    }

  }
  this.setData({
    totalMoney: this.data.totalMoney,
  })
},
/* 删除item */
delGoods: function (e) {
  this.data.carts.splice(e.target.id.substring(3),1);
  // 更新data数据对象  
  if (this.data.carts.length > 0) {
    this.setData({
      carts: this.data.carts
    })
    wx.setStorageSync('cart', this.data.carts);
    this.priceCount();
  } else {
    this.setData({
      cart: this.data.carts,
      havegoods: false,
     
    })
    wx.setStorageSync('cart', []);
  }
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this;
    var arr = wx.getStorageSync('cart') 

    if(arr.length>0){
      for (var j in arr) {
        arr[j].count=1;
        arr[j].totalMoney=arr[j].count * arr[j].price;
      }
      _this.setData({
        carts:arr,
        havegoods:true
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})